package cn.iocoder.yudao.module.vr.controller.admin.testgap;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.vr.controller.admin.testgap.vo.*;
import cn.iocoder.yudao.module.vr.convert.testgap.TestGapConvert;
import cn.iocoder.yudao.module.vr.dal.dataobject.testgap.TestGapDO;
import cn.iocoder.yudao.module.vr.service.testgap.TestGapService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 试题填空")
@RestController
@RequestMapping("/vr/test-gap")
@Validated
public class TestGapController {

    @Resource
    private TestGapService testGapService;
    @PostMapping("/create")
    @Operation(summary = "创建试题填空")
    @PreAuthorize("@ss.hasPermission('vr:test-gap:create')")
    public CommonResult<Long> createTestGap(@Valid @RequestBody TestGapCreateReqVO createReqVO) {
        return success(testGapService.createTestGap(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试题填空")
    @PreAuthorize("@ss.hasPermission('vr:test-gap:update')")
    public CommonResult<Boolean> updateTestGap(@Valid @RequestBody TestGapUpdateReqVO updateReqVO) {
        testGapService.updateTestGap(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试题填空")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vr:test-gap:delete')")
    public CommonResult<Boolean> deleteTestGap(@RequestParam("id") Long id) {
        testGapService.deleteTestGap(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试题填空")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vr:test-gap:query')")
    public CommonResult<TestGapRespVO> getTestGap(@RequestParam("id") Long id) {
        TestGapDO testGap = testGapService.getTestGap(id);
        return success(TestGapConvert.INSTANCE.convert(testGap));
    }

    @GetMapping("/list")
    @Operation(summary = "获得试题填空列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('vr:test-gap:query')")
    public CommonResult<List<TestGapRespVO>> getTestGapList(@RequestParam("ids") Collection<Long> ids) {
        List<TestGapDO> list = testGapService.getTestGapList(ids);
        return success(TestGapConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得试题填空分页")
    @PreAuthorize("@ss.hasPermission('vr:test-gap:query')")
    public CommonResult<PageResult<TestGapRespVO>> getTestGapPage(@Valid TestGapPageReqVO pageVO) {
        PageResult<TestGapDO> pageResult = testGapService.getTestGapPage(pageVO);
        return success(TestGapConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试题填空 Excel")
    @PreAuthorize("@ss.hasPermission('vr:test-gap:export')")
    @OperateLog(type = EXPORT)
    public void exportTestGapExcel(@Valid TestGapExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TestGapDO> list = testGapService.getTestGapList(exportReqVO);
        // 导出 Excel
        List<TestGapExcelVO> datas = TestGapConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "试题填空.xls", "数据", TestGapExcelVO.class, datas);
    }

}
